package bjut.zrpc.core.optimization.utils;



import bjut.zrpc.core.optimization.constant.ZRpcConstant;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * @Author: zhuzhiming
 * @Date: 2022/6/25 0:59
 */
public class ShutdownHookUtil {

    public static void shutdown() {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                String hostAddress = InetAddress.getLocalHost().getHostAddress();
                InetSocketAddress inetSocketAddress = new InetSocketAddress(hostAddress, ZRpcConstant.NETTY_SERVER_PORT);
                CuratorUtil.deleteAllService(inetSocketAddress);
            } catch (UnknownHostException e) {
                throw new RuntimeException(e.getMessage());
            }
        }));

        ThreadPoolUtil.shutdownAllThreadPool();

    }

}
